package com.dayuanit.dy15.atm.controller;

import com.dayuanit.dy15.atm.annotation.AutoWired;
import com.dayuanit.dy15.atm.annotation.Component;
import com.dayuanit.dy15.atm.annotation.RequestMapping;
import com.dayuanit.dy15.atm.dto.ResponseDTO;
import com.dayuanit.dy15.atm.entity.User;
import com.dayuanit.dy15.atm.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component("userController")
public class UserController {
    private UserService userService;

    @AutoWired("userServiceImpl")
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping(value = "/user/login.do")
    public ResponseDTO login(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //认证 也就是证明你是你
        User user = userService.login(username, password);

        //如果认证通过，那么就将你的信息保存到session对象里。
        //之所以保存到session对象中，是因为session对象生命周期比较长
        HttpSession session = request.getSession();
        session.setAttribute("loginUser", user);

        return ResponseDTO.sucess();
    }
}
